package com.eleadmin.order.service.impl;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.order.biz.ContainerBillBiz;
import com.eleadmin.order.biz.ContainerBillProcessBiz;
import com.eleadmin.order.entity.ContainerBill;
import com.eleadmin.order.entity.ContainerBillProcess;
import com.eleadmin.order.entity.WriteOff;
import com.eleadmin.order.service.ContainerBillService;
import com.eleadmin.order.service.WriteOffService;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;
import com.eleadmin.order.biz.WriteOffBiz;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 对账管理服务实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-08-11 16:47:23
 */
@Service
public class WriteOffServiceImpl /*extends ServiceImpl<WriteOffMapper, WriteOff> */ implements WriteOffService {

    @Autowired
    private WriteOffBiz writeOffBiz;
    @Autowired
    private ContainerBillBiz containerBillBiz;
    @Autowired
    private ContainerBillProcessBiz containerBillProcessBiz;
    @Autowired
    private ContainerBillService containerBillService;

    @Override
    public JsonResult list(PageResult2<T> page, WriteOff writeOff, User loginUser) throws Exception {
        return writeOffBiz.list(page, writeOff, loginUser);
    }

    @Override
    public JsonResult list(WriteOff writeOff, User loginUser) {
        return JsonResult.ok().setData(writeOffBiz.list(writeOff, loginUser));
    }

    @Override
    public JsonResult getById(String id) {
        return JsonResult.ok().setData(writeOffBiz.getById(id));
    }

    @Transactional
    @Override
    public JsonResult save(WriteOff writeOff, User loginUser) {
        if (writeOffBiz.save(writeOff, loginUser)) {
//            JSONArray ja = JSONArray.fromObject(writeOff.getBillIds());
//            for (int i = 0; i < ja.size(); i++) {
//                String id = ja.getJSONObject(i).getString("id");
//                ContainerBill cb = new ContainerBill();
//                cb.setId(id);
//                cb.setState("结单");
//                containerBillBiz.updateState(cb, loginUser);
//
//                ContainerBillProcess cbp = new ContainerBillProcess();
//                cbp.setBillId(id);
//                cbp.setState("结单");
//                containerBillProcessBiz.save(cbp, loginUser);
//            }
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(WriteOff writeOff, User loginUser) {
        if (writeOffBiz.update(writeOff, loginUser)) {
//            JSONArray ja = JSONArray.fromObject(writeOff.getBillIds());
//            for (int i = 0; i < ja.size(); i++) {
//                String id = ja.getJSONObject(i).getString("id");
//                ContainerBill cb = containerBillBiz.getById(id);
//                if(!cb.getState().equals("结单")) {
//                    cb = new ContainerBill();
//                    cb.setId(id);
//                    cb.setState("结单");
//                    containerBillBiz.updateState(cb, loginUser);
//
//                    ContainerBillProcess cbp = new ContainerBillProcess();
//                    cbp.setBillId(id);
//                    cbp.setState("结单");
//                    containerBillProcessBiz.save(cbp, loginUser);
//                }
//            }
//            //原来订单没有的要修改结单状态
//            WriteOff old = writeOffBiz.getById(writeOff.getId());
//            HashSet<String> diff = findDifferent(old.getBillIds(),writeOff.getBillIds());
//            Iterator<String> iterator = diff.iterator();
//            while (iterator.hasNext()) {
//                String element = iterator.next();
//                ContainerBill cb = new ContainerBill();
//                cb.setId(element);
//                cb.setState("应收费用审核");
//                containerBillService.updateState(cb, loginUser);
//            }
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult updateState(WriteOff writeOff, User loginUser) {
        if (writeOffBiz.updateState(writeOff, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult receiveState(WriteOff writeOff, User loginUser) {
        if (writeOffBiz.updateState(writeOff, loginUser)) {
            JSONArray ja = JSONArray.fromObject(writeOff.getBillIds());
            for (int i = 0; i < ja.size(); i++) {
                String id = ja.getJSONObject(i).getString("id");
                ContainerBill cb = new ContainerBill();
                cb.setId(id);
                cb.setState("结单");
                containerBillBiz.updateState(cb, loginUser);

                ContainerBillProcess cbp = new ContainerBillProcess();
                cbp.setBillId(id);
                cbp.setState("已收款");
                containerBillProcessBiz.save(cbp, loginUser);
                cbp.setState("结单");
                containerBillProcessBiz.save(cbp, loginUser);
            }
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    public HashSet<String> findDifferent(String old, String newer) {
        JSONArray oldsplit = JSONArray.fromObject(old);
        JSONArray newersplit = JSONArray.fromObject(newer);
        HashSet<String> set1 = new HashSet<>();
        HashSet<String> set2 = new HashSet<>();

        for (int i = 0; i < oldsplit.size(); i++) {
            String id = oldsplit.getJSONObject(i).getString("id");
            set1.add(id);
        }
        for (int i = 0; i < newersplit.size(); i++) {
            String id = newersplit.getJSONObject(i).getString("id");
            set2.add(id);
        }

        // 找出第一个数组中不在第二个数组中的元素
        HashSet<String> diff1 = new HashSet<>(set1);
        diff1.removeAll(set2);
//        System.out.println("第一个数组中不同的元素：" + Arrays.toString(diff1.toArray()));
        return diff1;
    }

    @Transactional
    @Override
    public JsonResult remove(String id, User loginUser) {
        if (writeOffBiz.remove(id, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
